#!/bin/bash

#
# configure -- by Dario Berzano <dario.berzano@cern.ch>
#
# This file is part of afdsmgrd -- see http://code.google.com/p/afdsmgrd
#
# Configure script that uses CMake to allow the building of afdsmgrd. This
# script is designed to:
#
#  - run as a standalone script called by the user for a custom build
#  - fit within the xrd-installer system if distributed with xrootd
#  - fit within the ROOT framework (--root-mode) if distributed with ROOT
#

#
# Globals
#

export PREFIX="/usr/local"
export ROOTSYS
export APMON_PREFIX
export APMON_DISABLED=0
export ROOT_MODE="FALSE"
export BUILD_TYPE="Release"

export BUILD_PREFIX="build"

#
# Functions
#

# Determine which configuration method is being called
function Main() {

  # To be sure we are in the project's tree root
  cd `dirname "$0"`

  CmakeExists
  if [ $? == 0 ]; then
    echo "*** No cmake in \$PATH: cmake is required! ***"
    exit 2
  fi

  if [ "$#" == 1 ] && [ "${1:0:2}" != "--" ]; then
    echo "*** Preparing configuration for xrd-installer ***"
    ConfigXrd "$1"
  elif [ "$1" == "--help" ]; then
    PrintHelp
    exit 1
  elif [ "$1" == "--clean" ]; then
    CmakeClean
    exit 0
  else
    echo "*** Preparing configuration for custom installation ***"
    ConfigCustom "$@"
  fi

  PrintConfig

  if [ -e "$BUILD_PREFIX" ]; then
    echo "*** Build directory already exists, remove it with --clean ***"
    exit 3
  fi

  local WD=`pwd`
  mkdir "$BUILD_PREFIX"
  cd "$BUILD_PREFIX"
  cmake .. \
    -DCMAKE_INSTALL_PREFIX=$PREFIX \
    -DCMAKE_BUILD_TYPE=$BUILD_TYPE \
    -DApMon_PREFIX=$APMON_PREFIX \
    -DApMon_DISABLED=$APMON_DISABLED \
    -DROOT_MODE=$ROOT_MODE
  local R=$?
  cd "$WD"
  if [ $R != 0 ]; then
    echo "*** Removing build directory ***"
    rm -rf "$BUILD_PREFIX"
    exit 4
  fi
  CreateSteerMake

  exit 0
}

# Prints the help
function PrintHelp() {
  echo "Parameters:"
  echo "  --prefix=<dir>        the installation prefix (default=/usr/local)"
  echo "  --with-rootsys=<dir>  the directory of ROOT (default=\$ROOTSYS)"
  echo "  --with-apmon=<dir>    the directory where to search for ApMon " \
    "libraries and includes (default=<auto>)"
  echo "  --disable-apmon       disable support for ApMon"
  echo "  --debug               debug symbols and test executables (disabled " \
    "by default)"
  echo "  --root-mode           used by ROOT if compiling as a part of it"
}

# Prints the configuration
function PrintConfig() {
  echo "BUILD_TYPE=$BUILD_TYPE"
  echo "PREFIX=$PREFIX"
  echo "ROOTSYS=$ROOTSYS"
  if [ $APMON_DISABLED == 0 ] && [ "$APMON_PREFIX" != "" ] ; then
    echo "APMON_PREFIX=$APMON_PREFIX"
  fi
  echo "ROOT_MODE=$ROOT_MODE"
}

# Checks for the presence of cmake in PATH; returns 1 on success, 0 on failure
function CmakeExists() {
  which cmake > /dev/null 2>&1 && return 1 || return 0
}

# Cleans the build tree and some other Cmake stuff
function CmakeClean() {
  echo "*** Cleaning up the project ***"
  rm -rf "$BUILD_PREFIX"
  rm -f Makefile
}

# Creates a Makefile that calls the Makefile in BUILD_DIR
function CreateSteerMake() {
  echo "*** Creating steer Makefile ***"
  cat > Makefile <<EOF
# Auto-generated by ./configure

.PHONY: all

all:
	@\$(MAKE) \$@ -C "$BUILD_PREFIX" --no-print-directory

.DEFAULT:
	@\$(MAKE) \$@ -C "$BUILD_PREFIX" --no-print-directory
EOF
}

# Configures variables if called from xrd-installer
function ConfigXrd() {
  PREFIX="$1"
  ROOTSYS=`cd "$PREFIX" 2>/dev/null && cd ../root 2>/dev/null && pwd`
  APMON_PREFIX=`cd "$PREFIX" 2>/dev/null && cd ../alien/api 2>/dev/null && pwd`
}

# Configures variables in a custom way
function ConfigCustom() {

  while [ $# -gt 0 ]; do
    case $1 in

      --prefix=*)
        PREFIX=${1:9}
      ;;

      --with-rootsys=*)
        ROOTSYS=${1:15}
      ;;

      --with-apmon=*)
        APMON_PREFIX=${1:13}
      ;;

      --disable-apmon)
        APMON_DISABLED=1
      ;;

      --debug)
        BUILD_TYPE="Debug"
      ;;

      --root-mode)
        ROOT_MODE="TRUE"
      ;;

      *)
        echo "Unrecognized option: $1" >&2
        exit 2
      ;;

    esac
    shift
  done
}

#
# Entry point
#

Main "$@"
